package com.maimao.auth.imageCode.mobile;

import com.maimao.auth.service.BaseUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.stereotype.Component;

/**
 * 用户名密码+图形验证码授权方式 总配置
 *
 * @author MaoLin Wang
 * @date 2020/12/14 3:57 下午
 */
@Component
public class MobilePwdCodeAuthenticationConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    @Autowired
    private BaseUserDetailsService baseUserDetailsService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    public PasswordEncoder bCryptPasswordEncoder;

    @Override
    public void configure(HttpSecurity http) throws Exception {

        // 构建一个MobileAuthenticationProvider实例，接收 mobileUserDetailsService 通过手机号查询用户信息
        MobilePwdCodeAuthenticationProvider provider = new MobilePwdCodeAuthenticationProvider();
        provider.setBaseUserDetailsService(baseUserDetailsService);
        provider.setRedisTemplate(stringRedisTemplate);
        provider.setPasswordEncoder(bCryptPasswordEncoder);
        http.authenticationProvider(provider);

    }
}

